{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 从MLP到卷积"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "考虑一个任务——分类猫和狗的图片。\n",
    "- 使用一个还不错的相机采集图片，RGB图片有36M元素\n",
    "- 使用100大小的单隐藏层MLP，隐藏层权重有3.6billion。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='cnn_MLP.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有什么办法能让参数的数量下降，还能保持比较好的效果呢？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以增加两个原则：\n",
    "- 平移不变性\n",
    "- 局部性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以把原始的图片（有高和宽），转换成另一张图片。即从(k,l)位置映射到(i,j)位置。\n",
    "\n",
    "$$h_{i,j} = \\Sigma_{k,l} {w_{i,j,k,l}x_{k,l}} = \\Sigma_{a,b} {v_{i,j,a,b}x_{i+a,j+b}}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$V$是W的重新索引，使得$v_{i,j,a,b} = w_{i,j,i+a,j+b}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原则一——平移不变性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- x的平移导致h的平移 $h_{i,j} = \\Sigma_{a,b} {v_{i,j,a,b}x_{i+a,j+b}}$\n",
    "- v不应该依赖于i，j\n",
    "因此，$v_{i,j,a,b} = v_{a,b}$（意味着并非所有的权重都是自由的，而是受到限制，降低了模型的复杂度）\n",
    "$$h_{i,j} = \\Sigma_{a,b} {v_{a,b}x_{i+a,j+b}}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这就是二维交叉相关（或卷积）。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原则二——局部性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 当我们评估$h_{i,j}$时，不应该用远离（i，j）的参数\n",
    "因此，当$|a|,|b|>\\delta$时，令$v_{a,b}=0$。\n",
    "$$h_{i,j} = \\Sigma_{a=-\\Delta}^\\Delta \\Sigma_{b=-\\Delta}^\\Delta {v_{a,b}x_{i+a,j+b}}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "至此，我们对全连接层应用两个原则，得到了卷积层。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='cnn_formula.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二维卷积层"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='cnn_0.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 输入形状 $X: n_h\\times n_w$\n",
    "- 核形状 $W: k_h\\times k_w$\n",
    "- 偏差 $b$\n",
    "- 输出形状 $Y: (n_h-k_h+1)\\times (n_w-k_w+1)$\n",
    "$$Y = X * W + b$$ \n",
    "或者表示成\n",
    "$$ y_{i,j} = \\Sigma_{a=1}^h \\Sigma_{b=1}^w {w_{a,b}x_{i+a,j+b}}$$\n",
    "- W 和 b是可学习的参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='cnn_2.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也有一维卷积层和三维卷积层"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='cnn_4.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "总结\n",
    "- 卷积层将输入和核矩阵进行交叉相关，加上偏移后得到输出\n",
    "- 核矩阵和偏移是可学习的参数\n",
    "- 核矩阵的大小是超参数，体现了局部性\n"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": true,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "vscode": {
   "interpreter": {
    "hash": "34418ffb6f02e522390adb0e13441cc75f901cd11cccb4f6f613643b4b4d2a0b"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
